#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int countPairs(vector<int>& nums, int k) {
        unordered_map<int, vector<int>> hash;
        int res = 0;
        int n = nums.size();
        for (int i = 0; i < n; ++i)
        {
            int num = nums[i];
            if (hash.find(num) != hash.end())
            {
                for (const auto& e : hash[num])
                {
                    if ((e * i) % k == 0)
                        ++res;
                }
            }
            hash[num].push_back(i);
        }
        return res;
    }
};